bits 16
;https://wiki.osdev.org/Disk_access_using_the_BIOS_(INT_13h)
readdisk:
    push bp
    mov bp,sp
    sub sp,12
    ;bp+6   = buffer
    ;bp+4   = param1=>LBA
    ;bp+2   = ret add
    ;bp     = last stack bp
    ;bp-2   = Number of Heads
    ;bp-4   = Sectors per Track
    ;bp-6   = temp
    ;bp-8   = Sector
    ;bp-10  = Head
    ;bp-12  = Cylinder

    ;Set AH to 8, DL to the BIOS drive number, and execute INT 0x13.
    ;The value returned in DH is the "Number of Heads" -1.
    ;AND the value returned in CL with 0x3f to get the "Sectors per Track".
    mov ah,0x08
    mov dl,0x80
    int 0x13
    movzx ax,dh   
    inc ax
    mov [bp-2],ax
    and cl,0x3f  
    movzx ax,cl
    mov [bp-4],ax

    ;Temp = LBA / (Sectors per Track)
    ;Sector = (LBA % (Sectors per Track)) + 1
    ;Head = Temp % (Number of Heads)
    ;Cylinder = Temp / (Number of Heads)

    ;DIV: 除法（无符号)
    ;DIV BX ; AX = AX / BX
    ;商（结果）将存储在 AL（对于 8 位操作）、AX（对于 16 位操作）或 EAX（对于 32 位操作）中。
    ;余数将存储在 AH（对于 8 位操作）、DX（对于 16 位操作）或 EDX（对于 32 位操作）中。

    ;[bp-6] = [bp+4] / [bp-4]
    ;[bp-8] = [bp+4] % [bp-4] + 1
    ;[bp-10] = [bp-6] % [bp-2]
    ;[bp-12] = [bp-6] / [bp-2]
    xor dx,dx
    mov ax,[bp+4]
    mov bx,[bp-4]
    div bx
    mov [bp-6],ax
    inc dx
    mov [bp-8],dx
    xor dx,dx
    mov ax,[bp-6]
    mov bx,[bp-2]
    div bx
    mov [bp-12],ax
    mov [bp-10],dx

    ;Reading sectors with a CHS address
    ;Cylinder = 0 to 1023 (maybe 4095), Head = 0 to 15 (maybe 254, maybe 255), Sector = 1 to 63

    ;Set AH = 2
    ;AL = total sector count (0 is illegal) -- cannot cross ES page boundary, or a cylinder boundary, and must be < 128; in some rare cases, even crossing track boundaries in a single request may not be allowed
    ;CH = cylinder & 0xff
    ;CL = Sector | ((cylinder >> 2) & 0xC0);
    ;DH = Head -- may include two more cylinder bits
    ;ES:BX -> buffer
    ;Set DL = "drive number" -- typically 0x80, for the "C" drive
    ;Issue an INT 0x13.

    mov ax ,[bp-12]
    and ax,0x00ff
    mov ch,al
    mov ax, [bp-12]
    shr ax,2
    and ax,0xc0
    or  ax,[bp-8]
    mov cl,al
    mov ax,[bp-10]
    mov dh,al
    mov bx,[bp+6]
    mov dl,0x80
    mov ah,2
    mov al,1
    int 0x13
    ;int 0x13调用成功=>ah=0 不成功=>ah!=0
    ;The carry flag will be clear if the read operation completed successfully 
    ;and set if there is any error during the transfer.
    ;AH should be set to 0 on success and non-zero on error.
    mov sp,bp
    pop bp
    ret



; .debug_msg:
;     db"debugdebug"
; .debug:
;     push 3
;     push 10
;     push .debug_msg
;     call logprint
;     add sp , 6
;     jmp $
